﻿using System;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using Tizer.Core.DTO;

namespace Tizer.Web
{
    /// <summary>
    /// Базовая страница
    /// </summary>
    public class BasePage : Page
    {
        public const string StrEmpty = "-";
        /// <summary>
        /// Получить текущий аккаунт
        /// </summary>
        public LoggedAccount CurrentAccount
        {
            get { return (LoggedAccount)HttpContext.Current.Session[WebConfigurationManager.AppSettings["LoggedAccount"]]; }
        }

        /// <summary>
        /// Установить текущий аккаунт
        /// </summary>
        /// <param name="loggedAccount"></param>
        public void SetCurrentAccount(LoggedAccount loggedAccount)
        {
            HttpContext.Current.Session[WebConfigurationManager.AppSettings["LoggedAccount"]] = loggedAccount;
        }

        /// <summary>
        /// Удалить аккаунт из сессии
        /// </summary>
        public void CurrenAccountClear()
        {
            HttpContext.Current.Session[WebConfigurationManager.AppSettings["LoggedAccount"]] = null;
        }

        /// <summary>
        /// Поиск котрола на странице
        /// </summary>
        /// <param name="control">Контрол, содержащий нужный контрол</param>
        /// <param name="id">ID контрола</param>
        /// <returns>Контрол</returns>
        public static Control FindControl(System.Web.UI.Control control, string id)
        {
            Control findedControl = control.FindControl(id);
            if (findedControl != null)
                return findedControl;
            foreach (Control c in control.Controls)
            {
                findedControl = FindControl(c, id);
                if (findedControl != null)
                    return findedControl;
            }
            return null;
        }

        /// <summary>
        /// Получить текущую культуру
        /// </summary>
        /// <returns></returns>
        public string GetCurrentCulture()
        {
            string currentSession = Convert.ToString(Session["Culture"]);

            if (String.IsNullOrEmpty(currentSession))
                Session["Culture"] = "ru-ru";

            if ((Session["Culture"].ToString() != "ru-ru") & (Session["Culture"].ToString() != "en-us"))
                Session["Culture"] = "ru-ru";

            return (string)Session["Culture"];
        }
    }
}